package com.example.bzh.gamecat.dao;

import android.content.Context;
import android.util.Log;

import com.example.bzh.gamecat.bean.HotTag;
import com.example.bzh.gamecat.bean.UserBean;
import com.example.bzh.gamecat.util.ORMliteHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2016/8/15.
 */
public class HotTagBeanDao {
    private ORMliteHelper orMliteHelper;
    private static Dao<HotTag, Integer> videoDao;
    private static HotTagBeanDao videoCollectionDAO;

    public HotTagBeanDao(Context context) {
        orMliteHelper = new ORMliteHelper(context);
        try {
            videoDao = orMliteHelper.getDao(HotTag.class);
            Log.i("table","lalala");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private static HotTagBeanDao getVideoCollectionInstance(Context context) {
        if (videoCollectionDAO == null) {
            videoCollectionDAO = new HotTagBeanDao(context);
        }

        return videoCollectionDAO;
    }

    public static boolean add(Context context, HotTag hotTag) {
        boolean flag = false;
        getVideoCollectionInstance(context);
        try {
            int state = videoDao.create(hotTag);
            if (state > 0) {
                flag = true;
            }
            Log.i("TAG", "add:" + state);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }


    public static void del(Context context, HotTag hotTag) {
        getVideoCollectionInstance(context);
        try {
            DeleteBuilder builder = videoDao.deleteBuilder();
            builder.where().eq("tag", hotTag.getTag());
            int delete = builder.delete();
            Log.i("TAG", "delete" + delete);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static List<HotTag> queryAll(Context context, HotTag hotTag) {
        getVideoCollectionInstance(context);
        List<HotTag> list = new ArrayList<>();
        try {
            list = videoDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<HotTag> queryOne(Context context, HotTag hotTag) {
        getVideoCollectionInstance(context);
        List<HotTag> list = new ArrayList<>();
        try {
            QueryBuilder builder = videoDao.queryBuilder();
            Log.i("TAG","hottag="+hotTag.getTag());
            builder.where().eq("phone", hotTag.getTag());
            list= builder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

}
